package com.sen.pro.switches.admin.content.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sen.common.struts.BaseAction;
import com.sen.common.util.Feedback;
import com.sen.pro.switches.core.entity.SiteLinks;
import com.sen.pro.switches.core.serivce.SiteLinksService;

/**
 * 站点地址管理Action
 * 
 * @author tian qingsen
 */
@Scope("prototype")
@Controller("linksManageAction")
@Namespace("/admin/content")
@InterceptorRefs(value = { @InterceptorRef(value = "switches-needsession-stack") })
public class LinksManageAction extends BaseAction {

	private static final long serialVersionUID = -8778996891029214911L;
	private static Logger LOG = Logger.getLogger(LinksManageAction.class);

	@Resource(name = "siteLinksService")
	private SiteLinksService siteLinksService;

	private SiteLinks link;

	@Action(value = "links_manage", results = { @Result(name = SUCCESS, location = "/WEB-INF/view/admin/content/links_manage.jsp") })
	public String linksManage() {
		List<SiteLinks> links = siteLinksService.queryAllSiteLinks();
		request.setAttribute("linksList", links);
		return SUCCESS;
	}

	@Action(value = "links_edit", results = { @Result(name = SUCCESS, location = "/WEB-INF/view/admin/content/links_edit.jsp") })
	public String linksEdit() {
		if (link != null && link.getLinkId() != null) {
			link = siteLinksService.getSiteLinksById(link.getLinkId());
		}
		return SUCCESS;
	}

	@Action("links_doadd")
	public String linksDoAdd() {
		if (link == null) {
			writeJson(Feedback.fail("您填写的信息不完整！"));
			return NONE;
		}
		siteLinksService.insertSiteLinks(link);
		writeJson(Feedback.success(""));
		return NONE;
	}
	
	@Action("links_dodelete")
	public String linksDoDelete() {
		if (link == null || link.getLinkId() == null) {
			writeJson(Feedback.fail("删除失败，站点地址不存在！"));
			return NONE;
		}
		siteLinksService.deleteSiteLinks(link.getLinkId());
		writeJson(Feedback.success("删除成功！"));
		return NONE;
	}
	
	@Action("links_doedit")
	public String linksDoEdit() {
		siteLinksService.updateSiteLinks(link);
		writeJson(Feedback.success(""));
		return NONE;
	}

	public SiteLinks getLink() {
		return link;
	}

	public void setLink(SiteLinks link) {
		this.link = link;
	}

}
